package com.example.ceramics.activity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.ceramics.R;
import com.example.ceramics.database.dao.IUserDao;
import com.example.ceramics.database.dao.UserDaoImpl;
import com.example.ceramics.entity.PwdSwitch;
import com.example.ceramics.entity.UserEntity;
import com.example.ceramics.util.StringUtils;

import java.util.HashMap;

public class RegisterActivity extends BaseActivity {

    private EditText etAccount;
    //    private EditText etName;
    private Button btnRegister;

    private PwdSwitch pwd1;
    private PwdSwitch pwd2;

    private IUserDao userDao;

    @Override
    protected int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        etAccount = findViewById(R.id.et_account);
//        etName = findViewById(R.id.et_name);
        pwd1 = new PwdSwitch(findViewById(R.id.et_pwd), findViewById(R.id.iv_pwd_switch1));
        pwd2 = new PwdSwitch(findViewById(R.id.et_confirm_pwd), findViewById(R.id.iv_pwd_switch2));
        btnRegister = findViewById(R.id.btn_register);
    }

    @Override
    protected void initData() {
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = etAccount.getText().toString().trim();
//                String name = etName.getText().toString().trim();
                String pwd = pwd1.etPwd.getText().toString().trim();
                String confirmPwd = pwd2.etPwd.getText().toString().trim();
                register(account, pwd, confirmPwd);
            }
        });
    }

    //注册
    private void register(String account, String pwd, String confirmPwd) {
        if (StringUtils.isEmpty(account)) {
            showToast("请输入账号!");
            return;
        }
        if (StringUtils.isEmpty(pwd)) {
            showToast("请输入密码!");
            return;
        }
        if (!pwd.equals(confirmPwd)) {
            showToast("两次输入密码不一致!");
            return;
        }

        userDao = new UserDaoImpl(mContext);
        UserEntity user = new UserEntity();
        user.setAccount(account);
        user.setPassword(pwd);
        int result = userDao.addUser(user);
        if (result==1) {
            navigateTo(LoginActivity.class);
            showToast("注册成功！");
        }else {
            showToast("该账号已被注册！");
        }

//        navigateTo(LoginActivity.class);
//        showToast("注册成功！");

        //网络请求
//        HashMap<String, Object> params = new HashMap<String, Object>();
//        params.put("userId", account);
////        params.put("name", name);
//        params.put("passWord", pwd);
//        Api.config(ApiConfig.REGISTER, params).postRequest(new MyCallback() {
//            @Override
//            public void onSuccess(String res) {
//                Log.e("onSuccess", res);
//
//                //把json字符串转转换为实体对象
//                Gson gson = new Gson();
//                LoginResponse registerResponse = gson.fromJson(res, LoginResponse.class);
//                if (registerResponse.getCode() == 200) {
//                    if(registerResponse.isData()){
//                        navigateTo(LoginActivity.class);
//                        showToastSync("注册成功！");}else {
//                        showToastSync("该账号已被注册!");
//                    }
//                } else {
//                    showToastSync("注册失败！");
//                }
//            }
//
//            @Override
//            public void onFailure(Exception e) {
//                Log.e("onFailure", e.toString());
//                showToastSync("请求失败！");
//            }
//        });
    }
}